package com.zc.im.common.r2dbc.domain;

import org.springframework.data.annotation.*;
import org.springframework.data.domain.Persistable;

import java.time.LocalDateTime;

/**
 * Project：im
 * Date：2022/4/13
 * Time：18:30
 * Description：基类
 *
 * @author Challen.Zhang
 * @version 1.0
 */
public class AbstractBase implements Persistable<Long> {
    /**
     * id
     */
    @Id
    @Persistent
    protected Long id;

    @CreatedDate
    protected LocalDateTime createdAt;

    @LastModifiedDate
    protected LocalDateTime updatedAt;

    @Transient
    protected boolean newObj;

    public AbstractBase() {
    }

    public AbstractBase(Builder builder) {
        this.id = builder.id;
        this.createdAt = builder.createdAt;
        this.updatedAt = builder.updatedAt;
        this.newObj = builder.newObj;
    }

    @Override
    public boolean isNew() {
        return newObj;
    }

    public static abstract class Builder<T extends Builder<T>> {

        protected Long id;

        protected LocalDateTime createdAt;

        protected LocalDateTime updatedAt;

        private boolean newObj;

        /**
         * 返回建造者本身  (子类)
         *
         * @return Builder
         */
        protected abstract T self();

        public abstract AbstractBase build();

        public Long getId() {
            return id;
        }

        public T id(Long id) {
            if (id != null) {
                this.newObj = true;
            }
            this.id = id;
            return self();
        }

        public LocalDateTime getCreatedAt() {
            return createdAt;
        }

        public T createdAt(LocalDateTime createdAt) {
            this.createdAt = createdAt;
            return self();
        }

        public LocalDateTime getUpdatedAt() {
            return updatedAt;
        }

        public T updatedAt(LocalDateTime updatedAt) {
            this.updatedAt = updatedAt;
            return self();
        }

    }

    @Override
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public boolean isNewObj() {
        return newObj;
    }

    public void setNewObj(boolean newObj) {
        this.newObj = newObj;
    }
}